﻿using Rescue.Services;
using RescueApp.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RescueApp.Repository;
using RescueApp.Models;
using RescueApp.UI;
using Rescue.UI.Common;

namespace Rescue.UI
{
    public partial class DispatchDetails : Form
    {


        private RescueContext db;
        private RescueRepos repos;
        private RescueApplyService srv;
        private RescueDispatchService srv2;
        public DispatchDetails()
        {
            InitializeComponent();
        }
        public DispatchDetails(RescueContext db)
        {
            InitializeComponent();
            this.repos = new RescueRepos();
            this.db = db;
            srv = new RescueApplyService(db, repos);
            srv2 = new RescueDispatchService(db, repos);
        }

        private void loadData(DataGridView dg)//显示派遣表
        {
            List<RescueDispatch> todoItems = srv2.QueryAllItems();
            dg.DataSource = todoItems;
            dg.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)//修改按钮
        {
            int idx;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择你想要修改的数据！", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            idx = dataGridView1.SelectedRows[0].Index;
            int Id = int.Parse(dataGridView1.Rows[idx].Cells[0].Value.ToString());
            string showId = dataGridView1.Rows[idx].Cells[0].Value.ToString();
            textBox1.Text = showId;
            var todoitem = db.RescueDispatches.FirstOrDefault(x => x.DispatchID == Id);
            if (todoitem == null)
            {
                MessageBox.Show($"not Found the Id:{Id} Item!");
                return;
            }

            DispatchUpdate frm = new DispatchUpdate(db, srv2, todoitem);//打开修改界面
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Form1_Load(this, null);
            }
            frm.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)//查询按钮
        {
            Rescuesolve frm = new Rescuesolve(db);
            frm.ShowDialog();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loadData(dataGridView1);
        }

        private void button4_Click(object sender, EventArgs e)//刷新按钮
        {
            loadData(dataGridView1);
        }

        private void button3_Click(object sender, EventArgs e)//删除按钮
        {
            int idx;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择你想要修删除的数据！", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            idx = dataGridView1.SelectedRows[0].Index;
            int Id = int.Parse(dataGridView1.Rows[idx].Cells[0].Value.ToString());
            string showId = dataGridView1.Rows[idx].Cells[0].Value.ToString();
            textBox1.Text = showId;

            if (MessageBox.Show($"确定要删除派遣ID:{Id} 吗?", "确认消息", MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                return;
            }
            if (!DbHelper.IsExistsWithORM2(db, Id))
            {
                MessageBox.Show($"该派遣ID:{Id} 不存在!");
                return;
            }
            var entity = db.RescueDispatches.FirstOrDefault(x => x.DispatchID == Id);
            if (entity != null)
            {
                try
                {

                    srv2.Delete(entity);
                    MessageBox.Show($"派遣ID:{Id} 删除成功!");
                    Form1_Load(this, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)//显示当前ID
        {


        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)//统计按钮
        {
            DateTime startDate = dtpStartDate.Value;
            DateTime endDate = dtpEndDate.Value;
            var stats = srv2.GetStatistics(startDate, endDate);
            textBox4.Text = $"{stats.TotalRescues}";
            textBox5.Text = $"{stats.TotalCost}";

        }
    }
}
